var str = '';
var index;
var numArr = [0];
localStorage.setItem("numArr",numArr);
var element, layer, util, $;
layui.use(['element', 'layer', 'util','jquery'], function(){
    element = layui.element,
        layer = layui.layer,
        util = layui.util,
        $ = layui.jquery;
    us($('input[name = user]').val());
    function us(data) {
        var a = data;
        var c = a.substring(a.indexOf('(')+1,a.indexOf(')')).split(', ');
        var d = {};
        for (var i = 0; i < c.length; i++) {
            var n = c[i].split('=');
            d[n[0]] = n[1];
        }
        localStorage.setItem("user",JSON.stringify(d));
    }

    //获取登录人员id
    var user = JSON.parse(localStorage.getItem("user"));
    var userId = user.userId;

    //获取导航栏数据
    getNavigationBarData(10);
    //获取登录头像和部门
    getJsUser(userId);
    //获取登录日志信息
    getLoginUser(userId);
    //获取各订单不同状态的订单数量
    getOrderQuantityStatus(userId);

    function getNavigationBarData(type) {
        $.ajax({
            url:'/navigationBarData/getNavigationBarData',
            method:'post',
            data:{"type": type},
            dataType:'json',
            success:function (data) {
                var vm = new Vue({
                    el:'#app',
                    data: {},
                    methods:{
                        //头部导航栏隐藏框的展示与隐藏
                        showUser(){
                            $('#user-div').css('display','inline-block');
                        },
                        hideUser(){
                            $('#user-div').css('display','none');
                        },
                        showMore(){
                            $('#more-div').css('display','inline-block');
                        },
                        hideMore(){
                            $('#more-div').css('display','none');
                        },
                        showNotice(){
                            $('#notice-div').css('display','inline-block');
                        },
                        hideNotice(){
                            $('#notice-div').css('display','none');
                        },
                        hideAll(){
                            $('#user-div').css('display','none');
                            $('#more-div').css('display','none');
                            $('#notice-div').css('display','none');
                        }
                    },
                    created(){}
                })

                var nav = data.data;
                var html = '';
                html +='<ul class="layui-nav layui-nav-tree"  lay-filter="nav-tree">';
                $.each(nav,function (i,item) {
                    html = html+'<li class="layui-nav-item">';
                    html += '<a href="javascript:;">'+item.icon+item.menuName+'</a>';
                    if(item.children.length > 0){
                        html += '<dl class="layui-nav-child">';
                        $.each(item.children,function (j,children) {
                            html+='<dd>'
                            html += '<a href="javascript:;">'+children.menuName+'</a>';
                            if(children.children.length > 0){
                                html += '<dl class="layui-nav-child">';
                                $.each(children.children,function (k,childrenItem) {
                                    html += '<dd>'+
                                        '<a href="javascript:;" onclick="hitEvent(this)" class="submenu" _href="'+childrenItem.url+'">'+childrenItem.menuName+'</a>' +
                                        '<a class="a2" style="display: none" class="submenu2" href="'+childrenItem.url+'">'+childrenItem.menuName+'</a>'+
                                        '</dd>';
                                });
                                html+= '</dl>';
                            }
                        });
                        html+= '</dl>' ;
                    }
                    html+= '</li>';
                });
                html+='</ul>';
                $(".layui-side-scroll").append(html);

                //更新渲染导航栏
                element.init();
                changeFontColor();

                //点击导航栏关闭其他导航栏
                $(".layui-nav-tree li").on("click","a",function (e) {
                    if($(this).parent("li").hasClass("closeDao")){
                        $(this).parent("li").removeClass("layui-nav-itemed");
                        $(this).parent("li").removeClass("closeDao");
                    }else{
                        $(this).parent("li").addClass("layui-nav-itemed").siblings("li").removeClass("layui-nav-itemed");
                        $(this).parent("li").addClass("closeDao").siblings("li").removeClass("closeDao");
                    }
                });

                $("#hitLi").on("click","li",function () {
                    var i = $(this).attr("lay-id");
                    var arr = localStorage.getItem("numArr");
                    i = "" + i + "";
                    numArr = arr.split(",");
                    if (i != numArr[numArr.length -1]){
                        numArr.push(i);
                    }
                    //console.log(numArr);
                    localStorage.setItem("numArr",numArr);
                });

                var accountHref;
                var h;
                var systemHomeHref;
                var systemHomeTitle;

                $("dd .a2").each(function () {
                    if ($(this).text() == "账户设置"){
                        accountHref = $(this).attr("href");
                        h = $(this).text();
                        return;
                    };
                    if ($(this).text() == "首页"){
                        systemHomeHref = $(this).attr("href");
                        systemHomeTitle = $(this).text();
                        return;
                    }
                    if ($(this).text() == "微圈列表"){
                        microCircleListHref = $(this).attr("href");
                        microCircleListTitle = $(this).text();
                        return;
                    }
                    if ($(this).text() == "商品列表"){
                        commodityListHref = $(this).attr("href");
                        commodityListTitle = $(this).text();
                        return;
                    }
                    if ($(this).text() == "用户列表"){
                        userListHref = $(this).attr("href");
                        userListTitle = $(this).text();
                        return;
                    }
                    if ($(this).text() == "轮播图列表"){
                        lunImgsListHref = $(this).attr("href");
                        lunImgsListTitle = $(this).text();
                        return;
                    }
                    if ($(this).text() == "课程列表"){
                        classListHref = $(this).attr("href");
                        classListTitle = $(this).text();
                        return;
                    }
                    if ($(this).text() == "后台人员列表"){
                        behindUserListHref = $(this).attr("href");
                        behindUserListTitle = $(this).text();
                        return;
                    }
                    if ($(this).text() == "交易记录图"){
                        transactionImgsListHref = $(this).attr("href");
                        transactionImgsListTitle = $(this).text();
                        return;
                    }
                    if ($(this).text() == "部门管理"){
                        departmentListHref = $(this).attr("href");
                        departmentListTitle = $(this).text();
                        return;
                    }
                    if ($(this).text() == "优惠券列表"){
                        couponListHref = $(this).attr("href");
                        couponListTitle = $(this).text();
                        return;
                    }
                });
                $("#accountHref").attr("href",accountHref);
                $("#systemHome").attr("href",systemHomeHref);
                $("#microCircleListHref").attr("href",microCircleListHref);
                $("#commodityListHref").attr("href",commodityListHref);
                $("#userListHref").attr("href",userListHref);
                $("#lunImgsListHref").attr("href",lunImgsListHref);
                $("#classListHref").attr("href",classListHref);
                $("#behindUserListHref").attr("href",behindUserListHref);
                $("#transactionImgsListHref").attr("href",transactionImgsListHref);
                $("#departmentListHref").attr("href",departmentListHref);
                $("#couponListHref").attr("href",couponListHref);

                //获取选项卡删除事件
                $(".layui-tab").on("click",function(e){
                    if($(e.target).is(".layui-tab-close")){
                        $(".rightmenu").css("display","none");
                    }
                });

                $(".layui-tab").on("mouseover",function(e){
                    if($(e.target).is(".layui-tab-close")){
                        var index = $(e.target).parent("li").attr("lay-id");
                        index = "" + index + "";

                        var arr = localStorage.getItem("numArr");
                        numArr = arr.split(",");

                        $(".layui-tab-close").on("click",function () {
                            var newNumArr = numArr.filter(function (item) {
                                if (item != index){
                                    return item;
                                }
                            })
                            localStorage.setItem("numArr",newNumArr);
                        });

                    }
                });

                rightClick();
                loading();
            }
        })
    }

    function getJsUser(id) {
        $.ajax({
            url:'/navigationBarData/getJsUser',
            method:'post',
            data:{"id": id},
            dataType:'json',
            success: function (data) {
                $("#headerImage").attr("src",data.data.acatarUrl);
                $("#loginDept").text(data.data.deptName);
                $("#userName").text(data.data.trueName);
            }
        })
    }

    function getLoginUser(id) {
        $.ajax({
            url:'/navigationBarData/getLoginUser',
            method:'post',
            data:{"id": id},
            dataType:'json',
            success: function (data) {
                if (data.data[0] != null){
                    $("#nowLoginTime").text(data.data[0].loginDate);
                    if (data.data[1] != null){
                        $("#oldLoginTime").text(data.data[1].loginDate);
                    }
                    $("#loginAddress").text(data.data[0].loginAddress);
                    $("#loginIp").text(data.data[0].loginIp);
                }
            }
        })
    }

    function getOrderQuantityStatus(id) {
        var orderNum = 0;
        //订单提示
        $.ajax({
            url: "/navigationBarData/getOrderQuantityStatus",
            methods: "post",
            data: {"id": id},
            dataType: "json",
            success: function (data) {
                if (data.data.noPayment != 0){
                    $(".headAlert").append("<div> <a style='display:inline;color: #333;'href='javascript:;'><div style='float: left;'>待付款订单</div><div class='count_right' style='float: right;margin-right: 20px;'><span class='count_left'>(</span><span class='fColor' style='color: red;'>"+ data.data.noPayment +"</span><span class='count_left'>)</span></div></a> </div>");
                    orderNum += data.data.noPayment;
                }
                if (data.data.havePayment != 0){
                    $(".headAlert").append("<div> <a style='display:inline;color: #333;'href='javascript:;'><div style='float: left;'>已付款订单</div><div class='count_right' style='float: right;margin-right: 20px;'><span class='count_left'>(</span><span class='fColor' style='color: red;'>"+ data.data.havePayment +"</span><span class='count_left'>)</span></div></a> </div>");
                    orderNum += data.data.havePayment;
                }
                if (data.data.noSendGoods != 0){
                    $(".headAlert").append("<div> <a style='display:inline;color: #333;'href='javascript:;'><div style='float: left;'>待发货订单</div><div class='count_right' style='float: right;margin-right: 20px;'><span class='count_left'>(</span><span class='fColor' style='color: red;'>"+ data.data.noSendGoods +"</span><span class='count_left'>)</span></div></a> </div>");
                    orderNum += data.data.noSendGoods;
                }
                if (data.data.haveSendGoods != 0){
                    $(".headAlert").append("<div> <a style='display:inline;color: #333;'href='javascript:;'><div style='float: left;'>已发货订单</div><div class='count_right' style='float: right;margin-right: 20px;'><span class='count_left'>(</span><span class='fColor' style='color: red;'>"+ data.data.haveSendGoods +"</span><span class='count_left'>)</span></div></a> </div>");
                    orderNum += data.data.haveSendGoods;
                }
                if (data.data.haveSignFor != 0){
                    $(".headAlert").append("<div> <a style='display:inline;color: #333;'href='javascript:;'><div style='float: left;'>已签收订单</div><div class='count_right' style='float: right;margin-right: 20px;'><span class='count_left'>(</span><span class='fColor' style='color: red;'>"+ data.data.haveSignFor +"</span><span class='count_left'>)</span></div></a> </div>");
                    orderNum += data.data.haveSignFor;
                }
                if (data.data.returnRequest != 0){
                    $(".headAlert").append("<div> <a style='display:inline;color: #333;'href='javascript:;'><div style='float: left;'>退货申请</div><div class='count_right' style='float: right;margin-right: 20px;'><span class='count_left'>(</span><span class='fColor' style='color: red;'>"+ data.data.returnRequest +"</span><span class='count_left'>)</span></div></a> </div>");
                    orderNum += data.data.returnRequest;
                }
                if (data.data.returning != 0){
                    $(".headAlert").append("<div> <a style='display:inline;color: #333;'href='javascript:;'><div style='float: left;'>退货中订单</div><div class='count_right' style='float: right;margin-right: 20px;'><span class='count_left'>(</span><span class='fColor' style='color: red;'>"+ data.data.returning +"</span><span class='count_left'>)</span></div></a> </div>");
                    orderNum += data.data.returning;
                }
                if (data.data.haveReturn != 0){
                    $(".headAlert").append("<div> <a style='display:inline;color: #333;'href='javascript:;'><div style='float: left;'>已退货订单</div><div class='count_right' style='float: right;margin-right: 20px;'><span class='count_left'>(</span><span class='fColor' style='color: red;'>"+ data.data.haveReturn +"</span><span class='count_left'>)</span></div></a> </div>");
                    orderNum += data.data.haveReturn;
                }
                if (data.data.cancelTransaction != 0){
                    $(".headAlert").append("<div> <a style='display:inline;color: #333;'href='javascript:;'><div style='float: left;'>取消交易</div><div class='count_right' style='float: right;margin-right: 20px;'><span class='count_left'>(</span><span class='fColor' style='color: red;'>"+ data.data.cancelTransaction +"</span><span class='count_left'>)</span></div></a> </div>");
                    orderNum += data.data.cancelTransaction;
                }
                //广告位提示
                $.ajax({
                    url:'/Advertisement/getAdvertisement',
                    methods: "post",
                    dataType:'json',
                    data: {"display": '', "imgType": ''},
                    success:function (data) {
                        var nowDate = new Date().getTime();
                        var n = 0;
                        for (var i = 0; i < data.data.length ; i++) {
                            //将日期转换为毫秒值
                            var expireDate = new Date( data.data[i].expireDate.replace(new RegExp("-","gm"),"/")).getTime();
                            if ((expireDate - nowDate) <= 172800000) {
                                n += 1;
                            }
                        }
                        if (n != 0){
                            $(".footAlert").append("<div> <a style='display:inline;color: #333;'href='javascript:;'><div style='float: left;'>广告位即将到期</div><div class='count_right' style='float: right;margin-right: 20px;'><span class='count_left'>(</span><span class='fColor' style='color: red;'>"+ n +"</span><span class='count_left'>)</span></div></a> </div>");
                            orderNum += n;
                            $(".layui-badge").text(orderNum);
                        }
                        changeFontColor();
                    }
                });
            }
        });
    }
});

//头部导航栏菜单消息鼠标悬停改变颜色
function changeFontColor() {
    $(".menu a,#notice-div a").on("mouseover",function () {
        $(this).css("color","#0DAB9E");
        $(this).find(".count_left").css("color","#0DAB9E");
        $(this).find(".fColor").css("color","#0DAB9E");
    });
    $(".menu a,#notice-div a").on("mouseout",function () {
        $(this).css("color","#000");
        $(this).find(".count_left").css("color","#000");
        $(this).find(".fColor").css("color","red");
    });
}

//侧边导航栏点击跳转并添加选项卡
function hitEvent(obj){
    var href = $(obj).attr('_href');
    var title = $(obj).text();
    var len = 0;
    if ($('.layui-tab-title li').length != 0){
        len = parseInt($(".layui-tab-title li").eq($('.layui-tab-title li').length-1).attr("lay-id")) + 1;
    } else{
        len = 0;
    }
    publicCode(title,href,len);
}

//头部导航栏点击跳转账户设置
function jumpAccount() {
    var title = $("#accountSettings").text();
    var href = $("#accountHref").attr("href");
    var len =  $('.layui-tab-title li').length;
    publicCode(title,href,len);
}

//头部常用菜单跳转商品列表
function jumpCommodityList() {
    var title = $("#commodityList").text();
    var href = $("#commodityListHref").attr("href");
    var len =  $('.layui-tab-title li').length;
    publicCode(title,href,len);
}

//头部常用菜单跳转微圈列表
function jumpMicroCircleList() {
    var title = $("#microCircleList").text();
    var href = $("#microCircleListHref").attr("href");
    var len =  $('.layui-tab-title li').length;
    publicCode(title,href,len);
}
//头部常用菜单跳转用户列表
function jumpUserList() {
    var title = $("#userList").text();
    var href = $("#userListHref").attr("href");
    var len =  $('.layui-tab-title li').length;
    publicCode(title,href,len);
}
//头部常用菜单跳转轮播图列表
function jumpLunImgsList() {
    var title = $("#lunImgsList").text();
    var href = $("#lunImgsListHref").attr("href");
    var len =  $('.layui-tab-title li').length;
    publicCode(title,href,len);
}

//头部常用菜单跳转课程列表
function jumpClassList() {
    var title = $("#classList").text();
    var href = $("#classListHref").attr("href");
    var len =  $('.layui-tab-title li').length;
    publicCode(title,href,len);
}

//头部常用菜单跳转后台人员列表
function jumpBehindUserList() {
    var title = $("#behindUserList").text();
    var href = $("#behindUserListHref").attr("href");
    var len =  $('.layui-tab-title li').length;
    publicCode(title,href,len);
}

//头部常用菜单跳转交易记录图
function jumpTransactionImgsList() {
    var title = $("#transactionImgsList").text();
    var href = $("#transactionImgsListHref").attr("href");
    var len =  $('.layui-tab-title li').length;
    publicCode(title,href,len);
}

//头部常用菜单跳转部门管理
function jumpDepartmentList() {
    var title = $("#departmentList").text();
    var href = $("#departmentListHref").attr("href");
    var len =  $('.layui-tab-title li').length;
    publicCode(title,href,len);
}

//头部常用菜单跳转优惠券列表
function jumpCouponList() {
    var title = $("#couponList").text();
    var href = $("#couponListHref").attr("href");
    var len =  $('.layui-tab-title li').length;
    publicCode(title,href,len);
}

//头部导航栏点击跳转首页
function jumpSystemHome() {
    var title = $("#systemHomeHit").attr("title");
    var href = $("#systemHome").attr("href");
    var len =  $('.layui-tab-title li').length;
    publicCode(title,href,len);
}

//导航栏公共代码
function publicCode(title,href,len) {
    var arr = [];
    $("#hitLi li").each(function () {
        arr.push($(this).text());
    });
    if (arr.indexOf(title) == -1){
        element.tabAdd('tab',{
            title: title,
            content: '<iframe frameborder="0" src="'+href+'"  class="iframe"></iframe>',
            id:len
        })
        //跳转添加选项卡页面
        element.tabChange('tab',len);
        var arr = localStorage.getItem("numArr");
        len = "" + len + "";
        numArr = arr.split(",");
        numArr.push(len);
        console.log(numArr);
        localStorage.setItem("numArr",numArr);
    }else {
        //跳转到点击选项卡页面
        var i = arr.indexOf(title);
        i = parseInt($(".layui-tab-title li").eq(i).attr("lay-id"));
        console.log(i);
        element.tabChange('tab',i);
        var arr = localStorage.getItem("numArr");
        i = "" + i + "";
        numArr = arr.split(",");
        if (i != numArr[numArr.length -1]){
            numArr.push(i);
        }
        localStorage.setItem("numArr",numArr);
    }
    rightClick();
}

//右键选项卡删除其他或所有
function rightClick() {
    $("#hitLi li").on("contextmenu",function (event) {
        //阻止浏览器默认右键操作
        event.preventDefault();
        $(".rightmenu").css("display","inline");
        index = $(this).attr("lay-id");
        var top = $("#hitLi .layui-this").position().top;
        var left = $("#hitLi .layui-this").position().left;
        top += 50;
        left += 20;
        $(".rightmenu").css('position','absolute');
        $(".rightmenu").css('top',top);
        $(".rightmenu").css('left',left);
    });

    $("#hitLi li").on("click",function () {
        $(".rightmenu").css("display","none");
    });

    $(".rightmenu").on("click","li",function () {
        if ($(this).attr("data-type") == "closeOthers") { //关闭其他
            var tabtitle = $(".layui-tab-title li");
            $.each(tabtitle, function (i) {
                if ($(this).attr("lay-id") != index) {
                    element.tabDelete('tab',$(this).attr("lay-id"));
                    $(".rightmenu").css("display","none");
                }
            })
            $("#hitLi ").prop("lay-id","0");
            console.log($(this).prop("lay-id"));
        }
        if ($(this).attr("data-type") == "closeAll") { //关闭所有
            var tabtitle = $(".layui-tab-title li");
            $.each(tabtitle, function (i) {
                element.tabDelete('tab',$(this).attr("lay-id"));
                $(".rightmenu").css("display","none");
            })
        }
    });
}


$("body").on("click",function () {
    $(".rightmenu").css("display","none");
})

//退出登录
function loginOut() {
    $.ajax({
        type:"post",
        url:"/login/loginOut",
        data:{},
        dataType:"json",
        success:function (data) {
            if(data.data){
                //退出到登录页面
                window.parent.location.href="/login/login.html";
            }
        }
    })
    return false;
}

//默认展开系统首页
function loading() {
    $(".layui-nav-tree li a").each(function () {
        if ($(this).text() == "系统首页"){
            $(this).parent("li").addClass("layui-nav-itemed").siblings("li").removeClass("layui-nav-itemed");
            $(this).parent("li").addClass("closeDao").siblings("li").removeClass("closeDao");
            $(this).siblings("dl").children("dd").addClass("layui-nav-itemed").siblings("li").removeClass("layui-nav-itemed");
        }
    });
}